package com.rjwh.dingdong.client.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aishua.lib.utils.AsStrUtils;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.bean.localbean.TagConfig;

/**
 * 帖子用户名标签view
 *
 * @author 王小珂
 * @ClassName TagLayout
 * @Description TODO(这里用一句话描述这个类的作用)
 * @date 2016-4-28 下午5:47:41
 * @history 1.YYYY-MM-DD author: description:
 */
public class NameTagView extends LinearLayout {

    private TextView view;

    public NameTagView(Context context) {
        super(context);
        init();
    }

    public NameTagView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        view = (TextView) inflate(getContext(), R.layout.username_tagview, null);
        LayoutParams params_rb = new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        params_rb.setMargins(dp2px(3), 0, 0, 0);
        addView(view, params_rb);
    }

    /**
     * 设置标签文字,背景色
     *
     * @param @param tagConfig
     * @param @param background
     * @param @param isStroke
     * @return void
     * @throws
     * @Title: setText
     * @Description: TODO(这里用一句话描述这个方法的作用)
     * @history 1.YYYY-MM-DD author: description:
     */
    public void setTag(TagConfig tagConfig, int background, boolean isStroke) {
        if (!AsStrUtils.isEmpty(tagConfig.getTag())) {
            view.setText(tagConfig.getTag());
        }
        setBackGround(tagConfig, background, isStroke);
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    public void setBackGround(TagConfig tagConfig, int background,
                              boolean isStroke) {
        view.setBackgroundResource(background);
        String bgColor = tagConfig.getBgcolor();
        String textColor = tagConfig.getColor();
        if (!AsStrUtils.isEmpty(bgColor) && !AsStrUtils.isEmpty(textColor)) {
            int redbg = Integer.valueOf(bgColor.split(",")[0]);
            int greenbg = Integer.valueOf(bgColor.split(",")[1]);
            int bluebg = Integer.valueOf(bgColor.split(",")[2]);
            int red = Integer.valueOf(textColor.split(",")[0]);
            int green = Integer.valueOf(textColor.split(",")[1]);
            int blue = Integer.valueOf(textColor.split(",")[2]);
            view.setTextColor(Color.rgb(red, green, blue));
            GradientDrawable d = (GradientDrawable) getContext().getResources()
                    .getDrawable(background);
            if (isStroke) {
                d.setStroke(2, Color.rgb(redbg, greenbg, bluebg));// 设置方框shape的stroke
            } else {
                d.setColor(Color.rgb(redbg, greenbg, bluebg));// 设置实心shape的背景填充颜色
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                view.setBackground(d);
            } else {
                view.setBackgroundDrawable(d);
            }
        }
    }

    public void setPadding(int left, int top, int right, int bottom) {
        view.setPadding(dp2px(left), dp2px(top), dp2px(right), dp2px(bottom));
    }

    public void setTextSize(int size) {
        view.setTextSize(TypedValue.COMPLEX_UNIT_SP, size);
    }

    private int dp2px(int dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
                getResources().getDisplayMetrics());
    }
}
